﻿using UnityEngine;
using System.Collections;
using System.Text;

public class stg2_showManager : MonoBehaviour {
    public Texture2D buttonImage = null;
    public static int[] mixCardNum = new int[11];
    public static int[] mixCardNum1 = new int[11];
    private int[] testWord = new int[6];
    public static int problem_manager;  //카드의 tag번호
    public static int game_type;
    public Texture2D fruit_btn;
    public Texture2D animal_btn;
    public Texture2D white_background;
    int k=1;
    int problem_num = 5;        //총 문제수
    int select_answer;          //몇번째 버튼을 클릭했는가
    int problem_count = 0;      //몇번째문제 푸는중인가
    int answer_count = 0;
    bool outKey = false;
    bool select_on = false;
    bool test_on = false;    
    bool btn, btn1, btn2, btn3, btn4;
    bool isFinish = false;
    bool select_btn1 = false;
    bool select_btn2 = false;

    float w = Screen.width;
    float h = Screen.height;

    string stringToEdit;
    string stringToEdit1;

    char[,] store_array = new char[11,11];
    string apple = "apple", grape = "grape", orange = "orange";
    string melon = "melon", strawberry = "strawberry", pear = "pear";
    string peach = "peach", watermelon = "watermelon", kiwi = "kiwi", banana = "banana";

    string cow = "COW", panda = "PANDA", pig = "PIG";
    string mouse = "MOUSE", tiger = "TIGER", deer = "DEER";
    string bear = "BEAR", rabbit = "RABBIT", monkey = "MONKEY", lion = "LION";


    string state1="";
    string state2="";
    string state3="";
    string state4="";

    public enum STATE { INIT, STAGE, TEST_SETTING, TEST_MODE, CHECK, CLEAR_PROBLEM, IDLE, FINISH }
    public static STATE state = STATE.INIT;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
        switch (state)
        {
            case STATE.STAGE:
                StartCoroutine("MakeStage2");
                break;
            case STATE.TEST_SETTING:
                test_setting();
                break;
            case STATE.TEST_MODE:
                StartCoroutine("make_test");
                break;
            case STATE.CHECK:
                test_check();
                break;
            case STATE.CLEAR_PROBLEM:
                clear_problem();
                break;
            case STATE.FINISH:
                finish();
                break;

        }
	}

    void insertArray(int j,string a)
    {
        for (int i = 0; i < a.Length; i++)
        {
            store_array[j, i] = a.ToCharArray()[i];            
        }
    }

    void mixCard()
    {
        for (var i = 1; i <= 10; i++)
        {
            mixCardNum[i] = k;		//카드 번호 순서대로 넣기
            k += 2;
        }
        k = 1;

        /*for (var i = 1; i <= 10; i++)
        {
            var n1 = Random.Range(1, 8);	//1-16까지 랜덤하게 선택
            var n2 = Random.Range(1, 8);

            var j = mixCardNum[n1];
            mixCardNum[n1] = mixCardNum[n2];
            mixCardNum[n2] = j;
        }*/
    }

    void mixCard1()
    {
        for (var i = 1; i <= 10; i++)
        {
            mixCardNum1[i] = k;		//카드 번호 순서대로 넣기
            k += 1;
        }
        k = 1;

        for (var i = 1; i <= 15; i++)
        {
            var n1 = Random.Range(1, 10);	//1-16까지 랜덤하게 선택
            var n2 = Random.Range(1, 10);

            var j = mixCardNum1[n1];
            mixCardNum1[n1] = mixCardNum1[n2];
            mixCardNum1[n2] = j;
        }
    }

    IEnumerator MakeStage2()     //StartCoroutine사용시 반드시 IEnumerator 사용
    {
        state = STATE.IDLE;
        mixCard();

        var x = -7.5f;
        var y = 3.0f;        
        for (var i = 1; i <= 10; i++)
        {
            var card = Resources.Load("Prefabs/word_card") as GameObject;    //prefabs로 카드 생성
            Instantiate(card, new Vector3(x, y, 5), transform.rotation);	//카드 위치 지정
            card.gameObject.tag = "CARD" + ((mixCardNum[i]/2) + 1);	//오브젝트에 tag 설정

            x += 3f;
            if (i % 5 == 0)
            {		//줄 바꿈
                x = -7.5f;
                y -= 5f;
            }
            yield return new WaitForSeconds(0.025f);    //0.25초 간격으로 카드 배열
        }
    }

    void test_setting()
    {
        state = STATE.IDLE;
        mixCard();
        if (game_type == 1)
        {
            insertArray(1, apple);
            insertArray(2, grape);
            insertArray(3, orange);
            insertArray(4, melon);
            insertArray(5, strawberry);
            insertArray(6, pear);
            insertArray(7, peach);
            insertArray(8, watermelon);
            insertArray(9, kiwi);
            insertArray(10, banana);
        }
        else if (game_type == 2)
        {
            insertArray(1, cow);
            insertArray(2, panda);
            insertArray(3, pig);
            insertArray(4, mouse);
            insertArray(5, tiger);
            insertArray(6, deer);
            insertArray(7, bear);
            insertArray(8, rabbit);
            insertArray(9, monkey);
            insertArray(10, lion);
        }
        
        
        
        for (var i = 1; i <= problem_num; i++)
        {
            testWord[i] = mixCardNum[i];
        }
        state = STATE.TEST_MODE;
    }

    IEnumerator make_test()
    {        
        state = STATE.IDLE;
        if (problem_count == problem_num)
        {
            state = STATE.FINISH;
        }
        if (state == STATE.IDLE)
        {
            problem_count++;
            var card = Resources.Load("Prefabs/word_test") as GameObject;   //그림생성
            Instantiate(card, new Vector3(0, 3.0f, 5), transform.rotation);
            card.gameObject.tag = "CARD" + testWord[problem_count];
            yield return new WaitForSeconds(1.0f);
            int problem_manger1 = (problem_manager / 2) + 1;

            while (!outKey)     //사지선다 섞기
            {
                mixCard1();
                for (var temp = 1; temp < problem_num; temp++)
                {
                    if (mixCardNum1[temp] == problem_manger1)     //섞인거에 그림에 해당하는 답이있으면 while문나가기
                    {
                        outKey = true;
                    }
                }
            }

            for (int i = 1; i <= 4; i++)
            {        //보기 4개
                int n = mixCardNum1[i];
                for (int k = 0; k < 11; k++)
                {
                    if (store_array[n, k] != null)
                    {
                        if (i == 1)
                        {
                            state1 += store_array[n, k].ToString();
                        }
                        if (i == 2)
                        {
                            state2 += store_array[n, k].ToString();
                        }
                        if (i == 3)
                        {
                            state3 += store_array[n, k].ToString();
                        }
                        if (i == 4)
                        {
                            state4 += store_array[n, k].ToString();
                        }
                    }
                }
            }
        }        
    }

    void test_check()
    {
        state = STATE.IDLE;
        int problem_manger1 = (problem_manager / 2) + 1;
        switch (select_answer)
        {
            case 1:
                if (mixCardNum1[1] == problem_manger1) answer_count++;
                else print(false);
                break;
            case 2:
                if (mixCardNum1[2] == problem_manger1) answer_count++;
                else print(false);
                break;
            case 3:
                if (mixCardNum1[3] == problem_manger1) answer_count++;
                else print(false);
                break;
            case 4:
                if (mixCardNum1[4] == problem_manger1) answer_count++;
                else print(false);
                break;
        }
        state = STATE.CLEAR_PROBLEM;
    }

    void clear_problem()
    {
        state = STATE.IDLE;
        stringToEdit = "";
        stringToEdit1 = "";
        isFinish = false;
        //stg2_wordTest.problem_script = 1;       //초기화
        btn1 = false;
        btn2 = false;
        btn3 = false;
        btn4 = false;
        outKey = false;
        state1 = "";
        state2 = "";
        state3 = "";
        state4 = "";
        clear();
        state = STATE.TEST_MODE;
    }

    void finish()
    {
        state = STATE.IDLE;
        clear();
        btn = false;
        test_on = false;
        problem_count = 0;
        isFinish = true;
    }

    void OnGUI()
    {
        GUIStyle style = new GUIStyle();
        style.fontSize = 35;
        style.normal.textColor = Color.black;
        style.normal.background = white_background;
        style.alignment = TextAnchor.MiddleCenter;

        var quit_btn = GUI.Button(new Rect(5 * w / 10, 7 * h / 9, 130, 90), "Go To Main!");
        if (quit_btn)
        {
            state = STATE.INIT;
            Application.LoadLevel("mainMenu");
        }
        if (state == STATE.INIT)
        {
            select_btn1 = GUI.Button(new Rect((2 * w / 5) - (w / 15), 4 * h / 9, h / 4, h / 4), fruit_btn);
            select_btn2 = GUI.Button(new Rect((3 * w / 5) - (w / 15), 4 * h / 9, h / 4, h / 4), animal_btn);
            if (select_btn1)
            {
                game_type = 1;
                state = STATE.STAGE;
                select_on = true;
            }
            if (select_btn2)
            {
                game_type = 2;
                state = STATE.STAGE;
                select_on = true;
            }
            
        }

        if (select_on)
        {
            btn = GUI.Button(new Rect((4 * w / 10) - (w / 20), 7 * h / 9, 130, 90), "TEST!");
            if (btn)
            {
                clear();
                test_on = true;
                state = STATE.TEST_SETTING;
            }
        }
        

        if (test_on)
        {
            btn1 = GUI.Button(new Rect(2 * w / 10, 6 * h / 9, 200, 80), "" + state1, style);
            btn2 = GUI.Button(new Rect((4 * w / 10) - (w / 20), 6 * h / 9, 200, 80), "" + state2, style);
            btn3 = GUI.Button(new Rect(5 * w / 10, 6 * h / 9, 200, 80), "" + state3, style);
            btn4 = GUI.Button(new Rect((7 * w / 10) - (w / 20), 6 * h / 9, 200, 80), "" + state4, style);
            if (btn1)
            {
                select_answer = 1;
                state = STATE.CHECK;
            }
            if (btn2)
            {
                select_answer = 2;
                state = STATE.CHECK;
            }
            if (btn3)
            {
                select_answer = 3;
                state = STATE.CHECK;
            }
            if (btn4)
            {
                select_answer = 4;
                state = STATE.CHECK;
                var ojj = GameObject.FindWithTag("CARD" + 1);
                ojj.animation.Play("wordTouch");
            }
        }

        if (isFinish)
        {
            GUIStyle style1 = new GUIStyle();
            style1.fontSize = 40;
            style1.normal.textColor = Color.black;
            string stringToEdit = "Number of total problems : " + problem_num;
            string stringToEdit1 = "Number of correct problems : " + answer_count;
            GUI.Label(new Rect(500, 100, 130, 50), stringToEdit, style1);
            GUI.Label(new Rect(500, 200, 130, 50), stringToEdit1, style1);
        }
    }

    void clear()
    {
        for (var i = 1; i < 20; i++)
        {
            var obj = GameObject.FindWithTag("CARD" + i);   //tag로 오브젝트 탐색
            Destroy(obj);
        }
    }
}
